package com.corewillsoft.fireflies.gamelaws;

import com.corewillsoft.fireflies.gameobjects.AreaObject;
import com.corewillsoft.fireflies.gameobjects.Firefly;
import com.corewillsoft.fireflies.gameobjects.GameObject;

public class WallFireflyLaw extends BaseLaw {

	@Override
	public void applicate(GameObject gameObject) {
		if (gameObject instanceof Firefly) {
			processFirefly((Firefly) gameObject);
		}
	}

	private void processFirefly(Firefly firefly) {
		AreaObject influenceArea = getArea();

        boolean isFireflyNearLeftEdge = influenceArea.isGameObjectNearLeftEdge(firefly);
        boolean isFireflyNearRightEdge = influenceArea.isGameObjectNearRightEdge(firefly);
        boolean isFireflyNearUpperEdge = influenceArea.isGameObjectNearUpperEdge(firefly);
        boolean isFireflyNearBottomEdge = influenceArea.isGameObjectNearBottomEdge(firefly);

        if (isFireflyNearLeftEdge || isFireflyNearRightEdge) {
			firefly.setSpeed(-firefly.getSpeedX(), firefly.getSpeedY());
            if (isFireflyNearLeftEdge) {
                firefly.setPosition(influenceArea.getMinX() - firefly.getArea().getWidth(), firefly.getY());
            }
            if (isFireflyNearRightEdge) {
                firefly.setPosition(influenceArea.getMaxX(), firefly.getY());
            }
		}

		if (isFireflyNearUpperEdge || isFireflyNearBottomEdge) {
			firefly.setSpeed(firefly.getSpeedX(), -firefly.getSpeedY());
            if (isFireflyNearUpperEdge) {
                firefly.setPosition(firefly.getX(), influenceArea.getMinY() - firefly.getArea().getHeight());
            }
            if (isFireflyNearBottomEdge) {
                firefly.setPosition(firefly.getX(), influenceArea.getMaxY());
            }
		}
    }

	@Override
	public LawType getType() {
		return LawType.WALL_FIREFLY;
	}
}
